/**
    ******************************************************************************
    * @file    sqlconnection.cpp
    * @author  debian
    * @version V1.0.0
    * @date    2020-03-08
    * @brief   sql链接程序
    ******************************************************************************
    * @attention
    *
    *
    ******************************************************************************
    */ 

/* Includes ------------------------------------------------------------------*/
#include "sqlconnection.h"
#include "string.h"

/** @addtogroup DataStruct_Driver
    * @{
    */

/** @addtogroup sqlconnection
    * @{
    */

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/


/** @defgroup sqlconnection_Exported_Functions sqlconnection Exported Functions
    * @{
    */

/** @defgroup sqlconnection_Exported_Functions_Group1 Initialization and deinitialization functions
    *  @brief    Initialization and Configuration functions
    *
@verbatim    
    ===============================================================================
                ##### Initialization and deinitialization functions #####
    ===============================================================================
    [..]
        This section provides functions allowing to initialize and de-initialize the sqlconnection
        to be ready for use.
 
@endverbatim
    * @{
    */ 

/**
    * @brief  Sqlconnection构造函数
    * @param  
    * @retval 
    */ 
Sqlconnection::Sqlconnection()
{
    _mysql = new MYSQL;
}    

/**
    * @brief  Sqlconnection析构函数
    * @param  
    * @retval 
    */ 
Sqlconnection::~Sqlconnection()
{
    if(_mysql) {
        mysql_close(_mysql);
        delete _mysql;
        _mysql = NULL;
    }
}
    
/**
    * @}
    */

/** @defgroup sqlconnection_Exported_Functions_Group2 operation functions 
    *  @brief   operation functions
    *
@verbatim   
    ===============================================================================
                        ##### operation functions #####
    ===============================================================================
    [..]
        This subsection provides a set of functions allowing to manage the sqlconnection.

@endverbatim
    * @{
    */

/**
    * @brief  Sqlconnection连接函数
    * @param  
    * @retval 
    */ 
bool Sqlconnection::connect(const char *host, const char *user, const char *passwd, const char *db, unsigned int port)
{
    _mysql = mysql_init(_mysql);
    if(!_mysql)  {
        printf("init %s\n", GetErrInfo());
        return false;
    }

    //尝试连接
    if(mysql_real_connect(_mysql, host, user, passwd, db, port, NULL, 0) == NULL ) {
        printf("connect %s\n", GetErrInfo());
        return false;
    }

    char cAuto = 1;
    if(!mysql_options(_mysql, MYSQL_OPT_RECONNECT, &cAuto)) {
        printf("option MYSQL_OPT_RECONNECT %s\n", GetErrInfo());
    }

    return true;
}


/**
    * @brief  Sqlconnection获取错误信息函数
    * @param  
    * @retval 
    */ 
const char* Sqlconnection::GetErrInfo()
{
    return mysql_error(_mysql);
}


void Sqlconnection::Reconnect()
{
    mysql_ping(_mysql);
}

void Sqlconnection::Close()
{
    mysql_close(_mysql);
    _mysql = NULL;
    printf("disconnect with mysql.");
}


bool Sqlconnection::Execyte(const char* sql)
{
    if(mysql_real_query(_mysql, sql, strlen(sql)) != 0 ) {
        if (mysql_errno(_mysql) == CR_SERVER_GONE_ERROR) {
            Reconnect();
	    }
        printf("execty %s\n", GetErrInfo());
        return false;
    }
    return true;
}

bool Sqlconnection::Execyte(const char* sql, SqlResult& res)
{
    if(mysql_real_query(_mysql, sql, strlen(sql)) != 0 ) {
        if (mysql_errno(_mysql) == CR_SERVER_GONE_ERROR) {
            Reconnect();
	    }
        printf("execty %s\n", GetErrInfo());
        return false;
    }

    MYSQL_RES* pRes = mysql_store_result(_mysql);
    if(!pRes)  {
        return NULL;
    }
    res.SerResult(pRes);

    return true;
}



bool Sqlconnection::CreatDataBase(const char* name)
{
    char cmd[100] = {0};

    snprintf(cmd, sizeof(cmd), "CREATE DATABASE %s;", name);
    return Execyte(cmd);
}

bool Sqlconnection::UseDataBase(const char* name)
{
    char cmd[100] = {0};

    snprintf(cmd, sizeof(cmd), "USE %s;", name);
    return Execyte(cmd);
}


MYSQL_ROW Sqlconnection::GetData(const char* tablename, const char* key, const char* value)
{
    char cmd[100] = {0};
    //SqlResult *res = new SqlResult();
    SqlResult res;

    snprintf(cmd, sizeof(cmd), "select * from %s where %s='啊甘';", tablename, key);
    Execyte(cmd, res);

    int count=res.GetRowCount();
    printf("count %d\n", count);
    MYSQL_ROW row = NULL;
    res.FetchRow(row);

    for(int i=0; i <res.RowCount(); i++ ) {
        printf("%s ", row[i]);
    }

    return NULL;
}

bool Sqlconnection::Transaction(char** sqls, int size)
{
    int ret = 0;
    ret = mysql_real_query(_mysql, "SET AUTOCOMMIT=0;", (unsigned int)strlen("SET AUTOCOMMIT=0;"));
    if(ret != 0)
    {
        printf("SET AUTOCOMMIT=0; flase\n");
        return false;
    }

    ret = mysql_real_query(_mysql, "begin;", (unsigned int)strlen("begin;"));
    if(ret != 0)
    {
        printf("begin\n");
        return false;
    }

    for(int i=0; i < size; i++)  {
        ret = mysql_real_query(_mysql, sqls[i], (unsigned int)strlen(sqls[i]));
        if(ret != 0)
        {
            printf("execute sql=%s failed\n", sqls[i]);
            mysql_real_query(_mysql, "rollback;", (unsigned int)strlen("rollback;"));
            return false;
        }
    }

    ret = mysql_real_query(_mysql, "commit;", (unsigned int)strlen("commit;"));
    if(ret != 0)
    {
        printf("commit\n");
        mysql_real_query(_mysql, "rollback;", (unsigned int)strlen("rollback;"));
        return false;
    }

    return true;
}




    /**
    * @}
    */


/**
    * @}
    */

/**
    * @}
    */

/**
    * @}
    */

/************************ (C) 2020 HOME 深圳龙华 *****END OF FILE****/

